package com.xxl.job.executor.service.jobhandler;

import com.xxl.job.core.biz.model.ReturnT;
import com.xxl.job.core.context.XxlJobContext;
import com.xxl.job.core.handler.annotation.XxlJob;
import com.xxl.job.executor.mybatis.service.JobUserService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.batch.core.*;
import org.springframework.batch.core.configuration.annotation.EnableBatchProcessing;
import org.springframework.batch.core.configuration.annotation.JobBuilderFactory;
import org.springframework.batch.core.configuration.annotation.StepBuilderFactory;
import org.springframework.batch.core.launch.JobLauncher;
import org.springframework.batch.core.scope.context.ChunkContext;
import org.springframework.batch.core.step.tasklet.Tasklet;
import org.springframework.batch.repeat.RepeatStatus;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;

import javax.annotation.Resource;
import java.util.Date;

import static com.xxl.job.core.handler.IJobHandler.SUCCESS;

/**
 * @author: 吴多强
 * @description: 一个Job多个Step
 **/
@Configuration
@EnableBatchProcessing
public class ShardingSpringBatchJob {

    private static Logger logger = LoggerFactory.getLogger(ShardingSpringBatchJob.class);
    @Resource
    private JobUserService jobUserService;

    @Autowired
    private JobLauncher jobLauncher;

    @Autowired
    private JobBuilderFactory jobBuilderFactory;

    @Autowired
    private StepBuilderFactory stepBuilderFactory;

    private int index, total, all, avg;


    @XxlJob("ShardingSpringBatchJob")
    public ReturnT<String> shardingJobHandler(String param) throws Exception {
        // 当前分片序号
        index = XxlJobContext.getXxlJobContext().getShardIndex() + 1;
        // 总分片数
        total = XxlJobContext.getXxlJobContext().getShardTotal();
        // 总的分片任务
        all = Integer.parseInt(param);
        // 平均分片个数
        avg = all / total;
        logger.info("当前分片序号" + index + "总分片数" + total + "总的分片任务" + all + "平均分片个数" + avg);
        JobParameters jobParameters = new JobParametersBuilder().addDate("date", new Date())
                .toJobParameters();
        jobLauncher.run(jobDemo(), jobParameters);
        return SUCCESS;
    }


    public Job jobDemo() {
        // 依次执行 on条件 to执行 from下一步 stopAndRestart 停止并重启
        return jobBuilderFactory
                .get("jobDemo")
                //.start(step1())
                //.next(step2())
                //.next(step3()).build();
                .start(step4()).on("COMPLETED")
                .to(step5()).from(step5()).on("COMPLETED")
                .to(step6()).from(step6())
                .end()
                .build();
    }

    public Step step1() {
        return stepBuilderFactory.get("step1").tasklet(new Tasklet() {
            @Override
            public RepeatStatus execute(StepContribution stepContribution, ChunkContext chunkContext) throws Exception {

                System.out.println("step1");
                return RepeatStatus.FINISHED;
            }
        }).build();
    }

    public Step step2() {
        return stepBuilderFactory.get("step2").tasklet(new Tasklet() {
            @Override
            public RepeatStatus execute(StepContribution stepContribution, ChunkContext chunkContext) throws Exception {
                System.out.println("step2");
                return RepeatStatus.FINISHED;
            }
        }).build();
    }

    public Step step3() {
        return stepBuilderFactory.get("step3").tasklet(new Tasklet() {
            @Override
            public RepeatStatus execute(StepContribution stepContribution, ChunkContext chunkContext) throws Exception {
                System.out.println("step3");
                return RepeatStatus.FINISHED;
            }
        }).build();
    }

    public Step step4() {
        return stepBuilderFactory.get("step4").tasklet(new Tasklet() {
            @Override
            public RepeatStatus execute(StepContribution stepContribution, ChunkContext chunkContext) throws Exception {
                System.out.println("step4");
                return RepeatStatus.FINISHED;
            }
        }).build();
    }

    public Step step5() {
        return stepBuilderFactory.get("step5").tasklet(new Tasklet() {
            @Override
            public RepeatStatus execute(StepContribution stepContribution, ChunkContext chunkContext) throws Exception {
                System.out.println("step5");
                return RepeatStatus.FINISHED;
            }
        }).build();
    }

    public Step step6() {
        return stepBuilderFactory.get("step6").tasklet(new Tasklet() {
            @Override
            public RepeatStatus execute(StepContribution stepContribution, ChunkContext chunkContext) throws Exception {
                System.out.println("step6");
                return RepeatStatus.FINISHED;
            }
        }).build();
    }
}
